#include "bsp.h"  

#define SPI_CHOOSE SPI1
#define SPI1_1 1
//#define SPI1_2 2
//#define SPI1_CHOOSE SPI1_1



void SPI1_SEND_BY_WAIT_Demo_GPIO_init(void)//??SPI1???GPIO
{
	RCC_PERCLK_SetableEx(PDCCLK, ENABLE);//??PAD??

	#ifdef SPI1_1
		AltFunIO(GPIOB, GPIO_Pin_12, 2);//PB12,SPI1_SSN
		//OutputIO(GPIOB, GPIO_Pin_12, 0);
		AltFunIO(GPIOB, GPIO_Pin_13, 0);//PB13,SPI1_SCK
		AltFunIO(GPIOB, GPIO_Pin_14, 0);//PB14,SPI1_MISO
		AltFunIO(GPIOB, GPIO_Pin_15, 0);//PB15,SPI1_MOSI
	#endif

	#ifdef SPI1_2
		AltFunIO(GPIOC, GPIO_Pin_6, 2);//PB12,SPI1_SSN
		//OutputIO(GPIOC, GPIO_Pin_6, 0);
		AltFunIO(GPIOC, GPIO_Pin_7, 0);//PB13,SPI1_SCK
		AltFunIO(GPIOC, GPIO_Pin_8, 0);//PB14,SPI1_MISO
		AltFunIO(GPIOC, GPIO_Pin_9, 0);//PB15,SPI1_MOSI
	#endif
}

void SPI2_SEND_BY_WAIT_Demo_GPIO_init(void)//??SPI2???IO
{
	RCC_PERCLK_SetableEx(PDCCLK, ENABLE);//??PAD??
	AltFunIO(GPIOD, GPIO_Pin_2, 2);//PB12,SPI1_SSN
	//OutputIO(GPIOD, GPIO_Pin_2, 0);
	AltFunIO(GPIOD, GPIO_Pin_3, 0);//PB13,SPI1_SCK
	AltFunIO(GPIOD, GPIO_Pin_4, 0);//PB14,SPI1_MISO
	AltFunIO(GPIOD, GPIO_Pin_5, 0);//PB15,SPI1_MOSI
}

unsigned char SPI1_SEND_BY_WAIT_Demo_RW(unsigned char address)//??len?????,???len?????
{
  return SPI_RW_Byte(SPI1,address);
}

void SPI2_SEND_BY_WAIT_Demo_RW(unsigned char address)//??len?????,???len?????
{
//	uint32_t i;
//	SPI_SSN_Set_Low(SPI2);//??SSN??
//	//GPIO_ResetBits(GPIOD, GPIO_Pin_2);
//	for (i = 0; i < len; i ++)
//	{
//		recv_buff[i] = SPI_RW_Byte(SPI2, send_buff[i]);
//	}
//	SPI_SSN_Set_High(SPI2);//??SSN??
//	//GPIO_SetBits(GPIOD, GPIO_Pin_2);
}

void SPI_SEND_BY_WAIT_Demo_RW(SPIx_Type* SPIx, unsigned char address)//??len?????,???len?????
{
	if (SPIx == SPI1)
	{
		SPI1_SEND_BY_WAIT_Demo_RW(address);
	}
	if (SPIx == SPI2)
	{
		SPI2_SEND_BY_WAIT_Demo_RW(address);
	}
}

void SPI1_SEND_BY_WAIT_Demo_Master_init(void)//SPI1??????
{
	SPI_Master_SInitTypeDef init_para;

	SPI1_SEND_BY_WAIT_Demo_GPIO_init();//??GPIO???

	RCC_PERCLK_SetableEx(SPI1CLK, ENABLE);//SPI???????

	init_para.BAUD_RATE = SPIx_SPICR1_BAUD_PCLK_16;//???????
	init_para.LSBF = SPIx_SPICR1_LSBF_MSB;//?bit??
	init_para.CPHOL = SPIx_SPICR1_CPHOL_HIGH;//????????
	init_para.CPHA = SPIx_SPICR1_CPHA_SECOND;//??????
	init_para.SSNM = SPIx_SPICR2_SSNM_LOW;//????8bit?Master??SSN?? 
	init_para.SSNSEN = SPIx_SPICR2_SSNSEN_SOFT;//????SSN

	SPI_Master_Init(SPI1, &init_para);//??????
	//SPI_SSN_Set_Low(SPI1);
}

void SPI2_SEND_BY_WAIT_Demo_Master_init(void)//SPI2??????
{
	SPI_Master_SInitTypeDef init_para;

	SPI2_SEND_BY_WAIT_Demo_GPIO_init();//??GPIO???

	RCC_PERCLK_SetableEx(SPI2CLK, ENABLE);//SPI???????

	init_para.BAUD_RATE = SPIx_SPICR1_BAUD_PCLK_4;//???????
	init_para.LSBF = SPIx_SPICR1_LSBF_MSB;//?bit??
	init_para.CPHOL = SPIx_SPICR1_CPHOL_HIGH;//????????
	init_para.CPHA = SPIx_SPICR1_CPHA_SECOND;//??????
	init_para.SSNM = SPIx_SPICR2_SSNM_LOW;//????8bit?Master??SSN?? 
	init_para.SSNSEN = SPIx_SPICR2_SSNSEN_SOFT;//????SSN

	SPI_Master_Init(SPI2, &init_para);//??????
	//SPI_SSN_Set_Low(SPI2);
}

void SPI_SEND_BY_WAIT_Demo_Master_init(SPIx_Type* SPIx)
{
	if (SPIx == SPI1)
	{
		SPI1_SEND_BY_WAIT_Demo_GPIO_init();
		SPI1_SEND_BY_WAIT_Demo_Master_init();
	}
	if (SPIx == SPI2)
	{
		SPI2_SEND_BY_WAIT_Demo_GPIO_init();
		SPI2_SEND_BY_WAIT_Demo_Master_init();
	}
}

//void SPI1_SEND_BY_WAIT_Demo_Master_Start(void)
//{
//	SPIx_SPICR2_SPIEN_Setable(SPI1, ENABLE);//SPI1????
//	SPI1_SEND_BY_WAIT_Demo_RW(SPI_Send_Buff, SPI_Recv_Buff, 8);//?????????
//}

//void SPI2_SEND_BY_WAIT_Demo_Master_Start(void)
//{
//	SPIx_SPICR2_SPIEN_Setable(SPI2, ENABLE);//SPI1????
//	SPI2_SEND_BY_WAIT_Demo_RW(SPI_Send_Buff, SPI_Recv_Buff, 1);//?????????
//}

//void SPI_SEND_BY_WAIT_Demo_Master_Start(SPIx_Type* SPIx)
//{
//  if (SPIx == SPI1)
//	{
//		SPI1_SEND_BY_WAIT_Demo_Master_Start();
//	}
//	if (SPIx == SPI2)
//	{
//		SPI2_SEND_BY_WAIT_Demo_Master_Start();
//	}
//}

